# $Id: Makefile 405 2014-03-06 04:33:08Z alan.watson.f $

OAXACADIR		=	/usr/local/oaxaca
PREFIX			=	$(OAXACADIR)

.PHONY			:	all
all			:	all-liboaxaca			\
				all-lib 			\
				all-bin				\
				all-libexec			\
				all-etc				\
				all-var				\
				all-www
.PHONY			:	install
install			:	install-liboaxaca		\
				install-lib			\
				install-bin			\
				install-libexec			\
				install-etc			\
				install-var			\
				install-www
.PHONY			:	uninstall
uninstall		:	uninstall-liboaxaca		\
				uninstall-lib			\
				uninstall-bin			\
				uninstall-libexec		\
				uninstall-etc			\
				uninstall-var			\
				uninstall-www
.PHONY			:	doc
doc			:	doc-libs
.PHONY			:	clean
clean			:	clean-liboaxaca			\
				clean-lib			\
				clean-libexec			\
				clean-etc			\
				clean-var
.PHONY			:	dist-clean
dist-clean		:	dist-clean-lib			\
				dist-clean-libexec
				
################################################################################

.PHONEY			:	all-lib
all-lib			:	all-libs

.PHONEY			:	install-lib
install-lib		:	all-lib install-libs

.PHONEY			:
uninstall-lib		:	uninstall-libs

COMPILED_LIBS 		= 	lib/oaxaca/foreign$(SO)
SCM_LIBS 		= 	lib/oaxaca/ao.sld 		\
				lib/oaxaca/console.sld		\
				lib/oaxaca/detector.sld		\
				lib/oaxaca/eval.sld		\
				lib/oaxaca/fcgi.sld		\
				lib/oaxaca/file-chibi.sld	\
				lib/oaxaca/file.sld		\
				lib/oaxaca/finder.sld		\
				lib/oaxaca/foreign.sld		\
				lib/oaxaca/housekeeping.sld	\
				lib/oaxaca/lamp.sld		\
				lib/oaxaca/load.sld		\
				lib/oaxaca/log.sld		\
				lib/oaxaca/main.sld		\
				lib/oaxaca/repl-chibi.sld	\
				lib/oaxaca/repl.sld		\
				lib/oaxaca/instrument.sld	\
				lib/oaxaca/telescope.sld	\
				lib/oaxaca/thread.sld		\
				lib/oaxaca/wfc.sld		\
				lib/oaxaca/wfs.sld

XCPPFLAGS 		+=	-Iliboaxaca 			\
				-I$(OAXACADIR)/include/
XCFLAGS 		+=	-g -O -Wall
XLIBS 			+=	-Lliboaxaca/ 			\
				-loaxaca 			\
				-lpthread			\
				-lrt				\
				-L$(OAXACADIR)/lib/

include Makefile.libs

################################################################################

BINS			=	bin/oaxaca			\
				bin/oaxaca-c-matrix		\
				bin/oaxaca-start		\
				bin/oaxaca-finder		\
				bin/oaxaca-fitsheadervalue	\
				bin/oaxaca-instrument		\
				bin/oaxaca-lamp			\
				bin/oaxaca-log			\
				bin/oaxaca-housekeeping		\
				bin/oaxaca-telescope		\
				bin/oaxaca-wfc			\
				bin/oaxaca-wfs

BIN_CFLAGS		=	-g -O -Wall -Werror -I$(OAXACADIR)/include/ -Iliboaxaca/ -std=c99
BIN_LIBS		=	-Lliboaxaca/ -loaxaca \
				-L"$(OAXACADIR)"/lib \
				-lchibi-scheme \
				-lcairo \
				-lueye_api \
				-lrt
				
.PHONY			:	all-bin
all-bin			:	$(BINS)

bin/oaxaca-finder	:	bin/oaxaca-finder.c 		\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-finder bin/oaxaca-finder.c $(BIN_LIBS)
		
bin/oaxaca-instrument	:	bin/oaxaca-instrument.c 	\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-instrument bin/oaxaca-instrument.c $(BIN_LIBS)

bin/oaxaca-lamp		:	bin/oaxaca-lamp.c 	\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-lamp bin/oaxaca-lamp.c $(BIN_LIBS)

bin/oaxaca-housekeeping	:	bin/oaxaca-housekeeping.c 	\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-housekeeping bin/oaxaca-housekeeping.c $(BIN_LIBS)

bin/oaxaca-wfc		:	bin/oaxaca-wfc.c 		\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-wfc bin/oaxaca-wfc.c $(BIN_LIBS)
		

bin/oaxaca-wfs		:	bin/oaxaca-wfs.c 		\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/oaxaca.h
	gcc $(BIN_CFLAGS) -o bin/oaxaca-wfs bin/oaxaca-wfs.c $(BIN_LIBS)

.PHONY			:	install-bin
install-bin		:	all-bin
	mkdir -p "$(OAXACADIR)"/bin
	cp -pf $(BINS) "$(OAXACADIR)"/bin
	
.PHONY			:	uninstall-bin
uninstall-bin		:
	for file in $(BINS); do rm -f $(OAXACADIR)/$$file; done

################################################################################

LIBEXECS		=	libexec/chibi-simple-fcgi
LIBEXEC_CLEANS		=	libexec/chibi-simple-fcgi-call-main.c
LIBEXEC_DIST_CLEANS	=

LIBEXEC_CFLAGS		=	$(BIN_CFLAGS)
LIBEXEC_LIBS		=	$(BIN_LIBS) -lfcgi 

.PHONY			:	all-libexec
all-libexec		:	$(LIBEXECS)

libexec/chibi-simple-fcgi	\
			:	libexec/chibi-simple-fcgi.c	\
				libexec/chibi-simple-fcgi-call-main.c
	gcc $(LIBEXEC_CFLAGS) -o libexec/chibi-simple-fcgi libexec/chibi-simple-fcgi.c $(LIBEXEC_LIBS)

libexec/chibi-simple-fcgi-call-main.c \
			:	libexec/chibi-simple-fcgi-call-main.scm
	sed 's/"/\\"/g;s/^/"/;s/$$/\\n"/' libexec/chibi-simple-fcgi-call-main.scm >libexec/chibi-simple-fcgi-call-main.c
	
.PHONY			:	install-libexec
install-libexec		:	all-libexec
	mkdir -p "$(OAXACADIR)"/libexec
	cp -pf $(LIBEXECS) "$(OAXACADIR)"/libexec

.PHONY			:	clean-libexec
clean-libexec		:
	-rm -f $(LIBEXECS) $(LIBEXEC_CLEANS)
	
.PHONY			:	dist-clean-libexec
dist-clean-libexec	:	clean-libexec
	-rm -f $(LIBEXECS_DIST_CLEANS)
	
################################################################################

LIBOAXACA_SRCS		=	liboaxaca/ao.c			\
				liboaxaca/array.c		\
				liboaxaca/detector.c		\
				liboaxaca/finder.c		\
				liboaxaca/housekeeping.c	\
				liboaxaca/ids.c			\
				liboaxaca/instrument.c		\
				liboaxaca/lamp.c		\
				liboaxaca/mprintf.c		\
				liboaxaca/oaxaca.c		\
				liboaxaca/rfits.c		\
				liboaxaca/shm.c			\
				liboaxaca/sleep.c		\
				liboaxaca/spots.c		\
				liboaxaca/wfc.c			\
				liboaxaca/wfits.c		\
				liboaxaca/wfs.c			\
				liboaxaca/wfs-loop.c		\
				liboaxaca/wpng.c

LIBOAXACA_HDRS		=	liboaxaca/ao.h			\
				liboaxaca/array.h		\
				liboaxaca/detector.h		\
				liboaxaca/finder.h		\
				liboaxaca/ids.h			\
				liboaxaca/instrument.h		\
				liboaxaca/mprintf.h		\
				liboaxaca/oaxaca.h		\
				liboaxaca/oaxaca-config.h	\
				liboaxaca/rfits.h		\
				liboaxaca/spots.h		\
				liboaxaca/wfits.h		\
				liboaxaca/wfs.h			\
				liboaxaca/wpng.h	

.PHONY			:	all-liboaxaca
all-liboaxaca		:	liboaxaca/liboaxaca.so

liboaxaca/liboaxaca.so	:	$(LIBOAXACA_SRCS) 		\
				$(LIBOAXACA_HDRS)
	gcc -fPIC -c -std=c99 $(XCFLAGS) $(XCPPFLAGS) $(LIBOAXACA_SRCS)
	gcc -shared -o liboaxaca/liboaxaca.so *.o
	-rm -f *.o

.PHONY			:	install-liboaxaca
install-liboaxaca	:	all-liboaxaca
	mkdir -p $(OAXACADIR)/lib
	cp -pf liboaxaca/liboaxaca.so $(OAXACADIR)/lib
	mkdir -p $(OAXACADIR)/include
	cp -pf liboaxaca/oaxaca.h $(OAXACADIR)/include

.PHONY			:	uninstall-liboaxaca	
uninstall-liboaxaca	:
	rm -f $(OAXACADIR)/include/oaxaca.h
	rm -f $(OAXACADIR)/lib/liboaxaca.so

.PHONY			:	clean-liboaxaca
clean-liboaxaca		:
	rm -f liboaxaca/liboaxaca$(SO)

################################################################################

ETCS			=	etc/lighttpd.conf	\
				etc/profile		\
				etc/rc.local		\
				etc/oaxaca.scm

.PHONY			:	all-etc
all-etc			:	$(ETCS)

.PHONY			:	install-etc
install-etc		:	all-etc
	mkdir -p "$(OAXACADIR)"/etc
	for file in $(ETCS); do cp -pf $$file "$(OAXACADIR)"/"$$file"; done

etc/lighttpd.conf	:	etc/lighttpd.conf.in
	sed 's:@OAXACADIR@:$(OAXACADIR):g' etc/lighttpd.conf.in >etc/lighttpd.conf
	
etc/profile		:	etc/profile.in
	sed 's:@OAXACADIR@:$(OAXACADIR):g' etc/profile.in >etc/profile
	
.PHONY			:	uninstall-etc
uninstall-etc		:
	for file in $(ETCS); do rm -f "$(OAXACADIR)"/"$$file"; done
	
.PHONY			:	clean-etc
clean-etc:
	rm -f etc/lighttpd.conf etc/profile
	
################################################################################

.PHONY			:	all-var
all-var			:

.PHONY			:	install-var
install-var		:	all-var
	mkdir -p "$(OAXACADIR)"/var
	mkdir -p "$(OAXACADIR)"/var/log
	mkdir -p "$(OAXACADIR)"/var/lighttpd
	mkdir -p "$(OAXACADIR)"/var/finder
	mkdir -p "$(OAXACADIR)"/var/instrument
	mkdir -p "$(OAXACADIR)"/var/wfs

.PHONY			:	uninstall-var
uninstall-var		:
	rm -rf "$(OAXACADIR)"/var

################################################################################

WWWS			=	www/console.html \
				www/detector-latest-image.cgi \
				www/index.html \
				www/jquery.js \
				www/log \
				www/oaxaca.js \
				www/sh \
				www/style.css

.PHONY			:	all-www
all-www			:	$(WWWS)

www/sh			:	www/sh.in
	sed 's:@OAXACADIR@:$(OAXACADIR):g' www/sh.in >www/sh
	chmod a+x www/sh

.PHONY			:	install-www
install-www		:	all-www
	mkdir -p "$(OAXACADIR)"/www
	for file in $(WWWS); do cp -pf $$file "$(OAXACADIR)"/"$$file"; done

.PHONY			:	uninstall-www
uninstall-www		:
	for file in $(WWWS); do rm -f "$(OAXACADIR)"/"$$file"; done
	
################################################################################

CHIBI_SCHEME_FLAGS	= PREFIX="$(OAXACADIR)"

.PHONY			:	all-chibi-scheme
all-chibi-scheme	:
	rm -rf chibi-scheme/
	#curl -Os http://chibi-scheme.googlecode.com/files/chibi-scheme-0.6.1.tgz
	#tar -xzf chibi-scheme-0.6.1.tgz
	#mv chibi-scheme*/ chibi-scheme
	hg clone https://code.google.com/p/chibi-scheme/
	#patch -c chibi-scheme/lib/chibi/repl.scm repl.diff
	make -C chibi-scheme $(CHIBI_SCHEME_FLAGS)
	make -C chibi-scheme $(CHIBI_SCHEME_FLAGS) test test-libs

.PHONY			:	install-chibi-scheme
install-chibi-scheme	:	all-chibi-scheme
	make -C chibi-scheme $(CHIBI_SCHEME_FLAGS) install

################################################################################

.PHONY			:	install-ueye-sdk
install-ueye-sdk	:	uninstall-ueye-sdk
	rm -rf ueye-sdk
	mkdir -p ueye-sdk
	cd ueye-sdk; curl -O http://en.ids-imaging.com/download-ueye.html?file=tl_files/downloads/uEye_SDK/driver/uEye_Linux_4.30_64_Bit.zip
	cd ueye-sdk; unzip *.zip
	cd ueye-sdk; sudo sh ueyesdk-setup-*-eth-*.gz.run
	sudo /etc/init.d/ueyeethdrc start
	echo 0 | ueyesetip; echo

.PHONY			:	uninstall-ueye-sdk
uninstall-ueye-sdk	:
	-test -f /usr/local/share/ueye/bin/ueyed_install-eth && \
	sudo /usr/local/share/ueye/bin/ueyed_install-eth uninstall

################################################################################

install-packages	:
	sudo apt-get -y install make gcc
	sudo apt-get -y install unzip
	sudo apt-get -y install mercurial libgc-dev libcairo-dev
	sudo apt-get -y install lighttpd haserl libfcgi libfcgi-dev
	sudo apt-get -y install octave

################################################################################

.PHONY			:	install-from-scratch
install-from-scratch	:	install-oaxacadir	\
				install-packages	\
				install-ueye-sdk	\
				install-chibi-scheme	\
				install-etc
	. $(OAXACADIR)/etc/profile; make install

.PHONY			:	install-oaxacadir
install-oaxacadir	:
	sudo mkdir -p "$(OAXACADIR)"
	sudo chown oaxaca:oaxaca "$(OAXACADIR)"